package zip;
import java.util.*;
import java.io.*;
import java.util.zip.*;

public class zipper
{
  
  public static void main (String[] args) 
  {
    /*
      arraylist of file names
      zip files
      delete files
      
    */ 
    String zipFileName = "bob.zip";
    String pathName = "C:/dev/java/zip/test/";
    
    
    ArrayList ar = new ArrayList();
    ar.add("Text2.txt");
    ar.add("Text3.txt");
    
     byte[] buf = new byte[1024];
    
    try {
        // Create the ZIP file
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
    
        // Compress the files
        for (int i=0; i<ar.size(); i++) {
            FileInputStream in = new FileInputStream(ar.get(i).toString());
    
            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(ar.get(i).toString()));
    
            // Transfer bytes from the file to the ZIP file
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
    
            // Complete the entry
            out.closeEntry();
            in.close();
        }
    
        // Complete the ZIP file
        out.close();
        
    } catch (IOException e) {
      System.out.println("Caught an exception while attempting to zip files");
      e.printStackTrace();
    }
    
    // delete the files
    for (int i=0; i<ar.size(); i++) {
      boolean success = (new File(ar.get(i).toString())).delete();
      if (!success) {
        // Deletion failed
        System.out.println("failed to delete file: " + ar.get(i).toString());
      }    
     }
      
    
  }
}